//1、找出元素item在给定个数组中的位置
function indexOfFn(arr,item){
    if(arr.indexOf(item)>0){
        return arr.indexOf(item);
    }else{
        return -1;
    }
}
console.log(indexOfFn([1,2,3,4],3));//2

function indexOfFn(arrr,item){
    if(arr.indexOfFn(item)>0){
        return arr.indexOfFn(item);
    }
    else{
        return -1;
    }
}
console.log([1,2,3],2);

function indexOfFn(arr,item){
    if(arr.indexOfFn(item)>0){
        return arr.indexOfFn(item);
    }else{
        return -1;
    }
}
console.log([1,2,3,4],3);
//2、数组求和
//数组中的元素均为 Number 类型

//yi eval：
function sum(arr) {
    return eval(arr.join("+"));
  };
  console.log(sum([ 1, 2, 3, 4 ])) //10

  function sum(arr){
      return eval(arr.join('+'));
  };
console.log(sum([,1,2,3]));

function sum(arr){
    return eval.join("+");
};
console.log([1,2,3,4]);

function sum(arr){
    return eval.join("+");
};
console.log([1,2,3,4]);

//常规循环：
function sum1(arr) {
    var s = 0;
    for (var i=arr.length-1; i>=0; i--) {
      s += arr[i];
    }
    return s;
  }
  console.log(sum1([ 1, 2, 3, 4 ])) //10

  function sun(arr){
      var s= 0;
      for(var i =0;i<arr.length-1;i--){
          s+=arr[i];
      }
      return s;
  }
  console.log([1,2,3,4]);

  function sun(arr){
      var s= 0;
      for(var i=arr.length-1;i>=0;i--){
          s+=arr[i];
      }
      return s;
  }
  console.log([1,2,3,4]);

  function sum(arr){
      var s= 0;
      for(var i=arr.length-1;i>=0;i--){
          s += arr[i];
      }
      return s;
  }
  console.log(sum[1,2,3,4]);

  function sum(arr){
      var s= 0;
      for(var i=arr.length;i>=0;i--){
          s+=arr[i];
      }
      return s;
  }
  console.log(sum[1,2,3,4]);
  //函数式编程 map-reduce：
  function sum2(arr) {
    return arr.reduce(function(prev, curr, idx, arr){
      return prev + curr;
    });
  }
  console.log(sum2([ 1, 2, 3, 4 ])) //10

  function sum(arr){
      return arr.reduce(function (prev,cuer){
          return prev+cuer;
      });
  }
  console.log(sum[1,2,3]);

  function sum(arr){
      return arr.reduce(function(hh,hhh){
          return hh+hhh;
      });
  }
  console.log(sum[1,2,3]);

  //forEach遍历：
  function sum3(arr) {
    var s = 0;
    arr.forEach(function(val, idx, arr) {
      s += val;
    }, 0);
    
    return s;
  };
  console.log(sum3([ 1, 2, 3, 4 ])) //10

  //不考虑算法复杂度，用递归做：
  function sum4(arr) {
    var len = arr.length;
    if(len == 0){
      return 0;
    } else if (len == 1){
      return arr[0];
    } else {
      return arr[0] + sum(arr.slice(1));
    }
  }
  console.log(sum4([ 1, 2, 3, 4 ])) //10

//3、移除数组中的元素
//修改Arr的length
let arr = [1,2,3,4,5,6,7,8,9];
arr.length = 4;
console.log('length',arr.length);
console.log('arr',arr);


let arr =[1,2,3,45,66,5,4];
arr.length =4;
console.log('length',arr.length);
console.log('arr',arr);

let arr =[1,2,33,45,56];
arr.length=2;
console.log('length',arr.length);
console.log('arr',arr);

let arr=[1,23,4,5,6,7];
arr.length=4;
console.log('length',arr.length);
console.log('arr',arr);

let arr =[1,2,3,46,7];
arr.length=4;
console.log('length',arr.length);
console.log('arr',arr);

//delete (修改Arr的内容,但length不变)
let arr1 = [1,2,3,4,5,6,7,8,9];
delete arr1[2];
console.log('length1',arr1.length);
console.log('arr1',arr1);

let arr = [1,2,34,66,8];
delete arr[2];
console.log('length',arr.length);
console.log('arr',arr);

let arr =[1,2,3,4,6];
delete arr[2];
console.log('length',arr.length);
console.log('arr',arr);

let arr =[1,2,34,5];
delete arr[2];
console.log('length',arr.length);
console.log('arr',arr);

//pop(栈方法,删除Arr的最后一个元素)
let arr2 = [1,2,3,4,5,6,7,8,9];
arr2.pop();
console.log('length2',arr2.length);
console.log('arr2',arr2);

let arr =[1,23,3,74,56];
arr.pop();
console.log('length',arr.length);
console.log('arr',arr);

let arr =[12,2,56,4];
arr.pop();
console.log('length',arr.length);
console.log('arr',arr);

//shift(删除Arr的第一位元素)
let arr3 = [1,2,3,4,5,6,7,8,9];
arr3.shift();
console.log('length3',arr3.length);
console.log('arr3',arr3);

let arr = [1,2,3,4];
arr.shift();
console.log('length',arr.length);
console.log('arr',arr);

let arr = [1,2,3,4];
arr.shift();
console.log('length',arr.length);
console.log('arr',arr);

//splice(可以对Arr进行增删改操作, 这里只写删除方法)
let arr4 = [1,2,3,4,5,6,7,8,9];
arr4.splice(0,2);
// 第一位参数 0: 代表的是起始下标注
// 第二位参数 2: 代表的是删除的位数
console.log('length4',arr4.length);
console.log('arr4',arr4);

//forEach || filter (通过迭代循环,删除元素)
let arr5 = [1,2,3,4,5,6,7,8,9];
arr5.forEach((item,index,arr) => {
    if(item === 3){
        arr5.splice(index,1)
    }
});
console.log('length5',arr5.length);
console.log('arr5',arr5);

//filter
let arr6 = [1,2,3,4,5,6,7,8,9];
arr6 = arr6.filter((item)=>{
    return item !== 3
});
console.log('length6',arr6.length);
console.log('arr6',arr6);

//操作原型(通过在Array原型上添加方法来完成删除)
let arr7 = [1,2,3,4,5,6,7,8,9];
Array.prototype.remove = function(v) {
    if(isNaN(v) || v > this.length){
        return false
    }
    for(let i = 0, j = 0; i < this.length; i++) {
        if(this[i] != this[v]){
            this[j++] = this[i]
        }
    }
    this.length -= 1
}
arr7.remove(2) //参数为下标
console.log('length7',arr7.length);
console.log('arr7',arr7);

////移除数组中的指定元素
//输出：remove([1, 2, 3, 4, 2], 2)

function remove(arr8, item) {
    var result=[];
       for(var i=0; i<arr8.length; i++){
       if(arr[i]!=item){
           result.push(arr8[i]);
       }
   }
    return result;
   }
   console.log(remove([1, 3, 4]));

   function count(arr,item){
       var result=[];
       for(var i = 0;i<arr.length;i++){
           if(arr[i]!=item){
               result.push(arr[i]);
           }
       }
       return result;
   }
console.log(count([1,3,4]));
//4、添加元素
function myFunction()
{
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    fruits.unshift("Lemon","Pineapple");
    var x=document.getElementById("demo");
    x.innerHTML=fruits;
}

//5、删除数组最后一个元素
//利用slice
function truncate(arr9) {
    return arr9.slice(0,-1);
}
console.log(truncate([1,2,3,4]));

//利用filter
function truncate2(arr10) {
    return arr10.filter(function(v,i,ar) {
        return i!==ar.length-1;
    });
}
console.log(truncate2([1,2,3,4,5,6]));

//利用push.apply+pop
function truncate3(arr11) {
    var newArr=[];
    [].push.apply(newArr, arr11);
    newArr.pop();
    return newArr;
}
console.log(truncate3([4,5,6,7]));

//利用join+split+pop    注意！！！：数据类型会变成字符型
function truncate4(arr12) {
    var newArr2 = arr12.join().split(',');
    newArr2.pop();
    return newArr2;
}
console.log(truncate4[4.5,8,6]);

//利用concat+pop
function truncate5(arr13) {
    var newArr3 = arr13.concat();
    newArr3.pop();
    return newArr3;
}
console.log(truncate5[4,56,6]);

//普通的迭代拷贝
function truncate6(arr14, item) {
    var newArr4=[];
    for(var i=0;i<arr14.length-1;i++){
        newArr4.push(arr14[i]);
    }
    return newArr4;
}
console.log(truncate6([1,47,56]));

//6、删除数组第一个元素
//利用slice
function curtail(arr15) {
    return arr15.slice(1);
}
console.log(curtail([1,2,34,54]));

//利用filter
function curtail33(arr16) {
    return arr16.filter(function(v,i) {
        return i!==0;
    });
}
console.log(curtail33([74,34,54]));

//利用push.apply+shift
function curtail2(arr17) {
    var newArr=[];
    [].push.apply(newArr, arr17);
    newArr.shift();
    return newArr;
}
console.log(curtail2([1,2,34,54]));

//利用join+split+shift    注意！！！：数据类型会变成字符型
function curtail3(arr18) {
    var newArr = arr18.join().split(',');
    newArr.shift();
    return newArr;
}
console.log(curtail4([2,34,54]));

//利用concat+shift
function curtail4(arr19) {
    var newArr = arr19.concat();
    newArr.shift();
    return newArr;
}
console.log(curtail4([1,54]));

//普通的迭代拷贝
function curtail5(arr20) {
    var newArr=[];
    for(var i=1;i<arr20.length;i++){
        newArr.push(arr20[i]);
    }
    return newArr;
}
console.log(curtail5([1,34,54]));

//7、数组合并
//concat 方法
var a=[1,2,3],b=[4,5,6];
var c=a.concat(b);
console.log(c);// 1,2,3,4,5,6
console.log(a);// 1,2,3  不改变本身

//循环遍历
var arr111=['a','b'];
var arr222=['c','d','e'];
 
for(var i=0;i<arr222.length;i++){
      arr111.push(arr222[i]) 
}
 
console.log(arr111);//['a','b','c','d','e']

//apply
//合并数组arr1和数组arr2,使用Array.prototype.push.apply(arr1,arr2)  or arr1.push.apply(arr1,arr2);
var arr1111=['a','b'];
var arr2222=['c','d','e'];
Array.prototype.push.apply(arr1111,arr2222);
//或者
//arr1.push.apply(arr1,arr2);<br>console.log(arr1) //['a','b','c','d','e']

//8、计数
//filter()
function count(arr44, item) {
    var count = arr44.filter(function(a) {
        return a === item;   //返回true的项组成的数组
    });
    return count.length;
}
console.log(count.arr44);// undefined

//map
function count(arr55,item){
    var count = 0;
    arr.map(function(b){
        if(a === item){
            count++;
        }
    });
    return count;
}
console.log(count([4,5,6,7,4,5,6],4));

//for循环
function count(arr66,item){
    var count = 0;
    for(var i=0;i<arr66.length;i++){
        if(arr66[i] === item){
            count++;
        }
    }
    return count;
}
console.log(count([4,55,36,5],5));

//reduce
function count(arr77,item){
    var count = arr77.reduce(function(prev,curr){
        return curr === item?prev+1:prev;
    },0);
    return count;
}
console.log(count([1,2,4,4,3,4,3],4));

//forEach
function count(arr,item){
    var count = 0;
    arr.forEach(function(c){
        c === item?count++:0;
    });
    return count;
}
console.log(count([1,1,24,7,7,],1));

//9、查找重复元素
function duplicates(arr) {
    var a=arr.sort(),b=[];
    for(var i in a){
        if(a[i]==a[i-1] && b.indexOf(a[i])==-1)
            b.push(a[i]); 
    }
    return b;
}
console.log(duplicates([1,1,2,34,5,6,2,4,2]));

//10、求二次方
//1
function square(arr){
    var a = arr.slice(0);
    a = a.map(function(val){
        return val*val;
    });
    return a;
}
console.log(square([1,2,3,4]));

//11、查找元素位置
//filter
function findAllOccurrences(arr, target) {
    var result=[];
    arr.filter(function(item,index){
        return item===target&&result.push(index);
    });
    return result;
}
console.log(findAllOccurrences(['abcdfabc']));